# Copyright 2015 Free Software Foundation, Inc.
#
# Contributed by Intel Corp. <walfred.tedeschi@intel.com>,
#                            <mircea.gherzan@intel.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if { ![istarget i?86-*-*] && ![istarget x86_64-*-* ] } {
    verbose "Skipping x86 MPX tests."
    return
}

standard_testfile

set comp_flags "-fmpx -I${srcdir}/../nat/"

if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
 [list debug nowarnings additional_flags=${comp_flags}]] } {
    return -1
}

if ![runto_main] {
	untested "could not run to main"
	return -1
}

set supports_mpx 0
set test "probe MPX support"

gdb_test_multiple "print have_mpx()" $test {
    -re ".. = 1\r\n$gdb_prompt $" {
        pass $test
        set supports_mpx 1
    }
    -re ".. = 0\r\n$gdb_prompt $" {
        pass $test
    }
}

if { !$supports_mpx } {
    unsupported "processor does not support MPX"
    return
}

gdb_breakpoint [ gdb_get_line_number "after-decl" ]
gdb_breakpoint [ gdb_get_line_number "after-alloc" ]
gdb_breakpoint [ gdb_get_line_number "after-assign" ]

gdb_test "show mpx bound 0x0" "Invalid bounds directory entry at $hex." "NULL address of the pointer"

gdb_continue_to_breakpoint "after-decl" ".*after-decl.*"
gdb_test "show mpx bound a" "Invalid bounds directory entry at $hex." "pointer instead of pointer address"

gdb_continue_to_breakpoint "after-alloc" ".*after-alloc.*"
gdb_test "show mpx bound a" "\\\{lbound = $hex, ubound = $hex\\\}: pointer value = $hex, size = \[8, 4\], metadata = 0x0+" "pointer after allocation"

gdb_continue_to_breakpoint "after-assign" ".*after-assign.*"
gdb_test "show mpx bound x" "\\\{lbound = $hex, ubound = $hex\\\}: pointer value = $hex, size = \[8, 4\], metadata = 0x0+" "pointer after assignment"
gdb_test "set mpx bound 0x0, 0x1, 0x2" "Invalid bounds directory entry at $hex." "set mpx bound: NULL address of the pointer"
gdb_test_no_output "set mpx bound x, 0xcafebabe, 0xdeadbeef" "set mpx bound: set bounds for a valid pointer address"
gdb_test "show mpx bound x" "\\\{lbound = .*cafebabe, ubound = .*deadbeef\\\}: pointer value = $hex, size = $decimal, metadata = 0x0+" "set mpx bound: bounds map entry after set mpx bound"


gdb_test "set mpx bound 0x0, 0x1 0x2" "A syntax error in expression.*" "set mpx bound: Controlling syntax error, missing comma "
gdb_test "set mpx bound 0x0, 0x1" "Wrong number of arguments.*" "set mpx bound: Controlling syntax error, missing argument "
